'''
Created 7/9/12
Modified 7/10/12

Instantiates Whole Brain Catalog SparseValueVolumeImporter

Run via jython
@author: drlittle
'''

# import Java class
from java.net import URI

# import Java classes from WBC
from org.wholebrainproject.wbc.data.importer import SparseValueVolumeImporter
from org.wholebrainproject.wbc.generated.sparsevaluevolume import SparseValueVolume

import pickle

'''
SCRIPT BEGINS HERE
'''

# uri to obtain gene expression sparse value volume data from Allen Brain Institute via INCF ABA hub process
svv_uri_string = "http://incf-dev-local.crbs.ucsd.edu/aba/atlas?service=WPS&version=1.0.0&request=Execute&Identifier=GetGeneExpressionByGeneId&DataInputs=geneIdentifier=Coch&RawDataOutput=SparseValueVolumeXML" 

# instantiate Java uri object
svv_uri = URI(svv_uri_string)
print 'uri:', svv_uri.toString()

# instantiate WBC importer
svv_importer = SparseValueVolumeImporter(svv_uri)

# get SparseValueVolume object
sparse_value_volume = svv_importer.getData()

# svv comment string
print 'Sparse Value Volume comment:', sparse_value_volume.getComment()

# get max dimensions -- these values are not accurate!
max_dimension_type = sparse_value_volume.getMaxDimension()
print 'Max dim x, y, z:', max_dimension_type.getX(), max_dimension_type.getY(), max_dimension_type.getZ()

# get all svv data
sparse_volume_data_type = sparse_value_volume.getSparseVolumeData()

# get list of svv datum (x, y, z, value)
datum_type = sparse_volume_data_type.getData()

# get actual max dimensions
xMax = 0
yMax = 0
zMax = 0 
for svd in datum_type:
    x = svd.getX()
    y = svd.getY()
    z = svd.getZ()
    if (x > xMax):
        xMax = x
    if (y > yMax):
        yMax = y
    if (z > zMax):
        zMax = z
print 'xMax, yMax, zMax:', xMax, yMax, zMax

# fill the array with 0s (float)
# note order of indices (z, y, x) in building array
# array indices are zero-based (0..xMax, 0..yMax, 0..zMax)
print 'Creating array of zero values'
a = [ [ [ 0. for z in range(zMax + 1)] for y in range(yMax + 1)] for x in range(xMax + 1)]

# fill in non-zero values for coordinates x, y, z from sparse volume data
print 'Filling non-zero values'
for svd in datum_type:
    a[svd.getX()][svd.getY()][svd.getZ()] = svd.getValue()

# dump full array 'a' (zero and non-zero values from sparse volume data) to text file
# file is for audit and trouble shooting
print 'Writing 3dExpressionData.txt'
f = open('3dExpressionData.txt', 'w')
f.write('i : x y z : expression value\n')
i = 0
for x in range(xMax + 1):
    for y in range(yMax + 1):
        for z in range(zMax + 1):
            f.write(str(i))
            f.write(' : ')
            f.write(str(x))
            f.write(' ')
            f.write(str(y))
            f.write(' ')
            f.write(str(z))
            f.write(' : ')
            f.write(str(a[x][y][z]))
            f.write('\n')
            i += 1
f.close()

# pickle array 'a'
print 'Pickling 3dExpressionData.pkl'
output = open('3dExtressionData.pkl', 'wb')
pickle.dump(a, output)
output.close()

# unpickle array into 'b'
print 'Unpickling 3dExpressionData.pkl'
pkl_file = open('3dExtressionData.pkl', 'rb')
b = pickle.load(pkl_file)
pkl_file.close()

# dump full array 'b' (zero and non-zero values from sparse volume data) to text file
# file is for audit and trouble shooting
print 'Writing 3dExpressionData1.txt'
f = open('3dExpressionData1.txt', 'w')
f.write('i : x y z : expression value\n')
i = 0
for x in range(xMax + 1):
    for y in range(yMax + 1):
        for z in range(zMax + 1):
            f.write(str(i))
            f.write(' : ')
            f.write(str(x))
            f.write(' ')
            f.write(str(y))
            f.write(' ')
            f.write(str(z))
            f.write(' : ')
            f.write(str(b[x][y][z]))
            f.write('\n')
            i += 1
f.close()


